package com.yonyou.iuap.shell.pub;

import java.io.IOException;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.yonyou.iuap.base.utils.BaseToolsUtils;
import com.yonyou.iuap.utils.PropertyUtil;

public class ShellFtpFileUtils {
	
	private static FTPClient ftpClient = null;
	 /**
	  * 获取FTP连接对象
	  * @return
	  */
	 public static FTPClient getFtpClient(){
		 
		 final String ftp_ip = BaseToolsUtils.getPropertiesValue("encrypt_ftp_ip");
		 final int ftp_port = StringUtils.isEmpty(BaseToolsUtils.getPropertiesValue("encrypt_ftp_port"))?21:Integer.valueOf(BaseToolsUtils.getPropertiesValue("encrypt_ftp_port"));
		 final String ftp_user = BaseToolsUtils.getPropertiesValue("encrypt_ftp_user");
		 final String ftp_pwd = BaseToolsUtils.getPropertiesValue("encrypt_ftp_pwd");
		 final String ftp_path = BaseToolsUtils.getPropertiesValue("encrypt_ftp_path");
		 if (ftpClient == null) {
				int reply;
				try {
					ftpClient = new FTPClient();
					ftpClient.setDefaultPort(ftp_port);
					// ftpClient.configure(getFtpConfig());
					ftpClient.connect(ftp_ip);
					ftpClient.login(ftp_user, ftp_pwd);
					ftpClient.setDefaultPort(ftp_port);
					reply = ftpClient.getReplyCode();

					if (!FTPReply.isPositiveCompletion(reply)) {
						ftpClient.disconnect();
						System.err.println("FTP server refused connection.");
					}
					ftpClient.setControlEncoding("UTF-8");
					ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); 
					ftpClient.enterLocalPassiveMode(); 
					ftpClient.setBufferSize(1024*1024*10);
					ftpClient.changeWorkingDirectory(ftp_path);
					return ftpClient;
				} catch (Exception e) {
					e.printStackTrace();
					return null;
				}
			}
		 
			return ftpClient;
		 
	 }
	 
	 /** 
	     * 连接sftp服务器 
	     * @param host 主机 
	     * @param port 端口 
	     * @param username 用户名 
	     * @param password 密码 
	     * @return 
	     */  
	    public static  ChannelSftp sftpConnect() {  
	    	 final String ftp_ip = BaseToolsUtils.getPropertiesValue("encrypt_ftp_ip");
			 final int ftp_port = StringUtils.isEmpty(BaseToolsUtils.getPropertiesValue("encrypt_ftp_port"))?21:Integer.valueOf(BaseToolsUtils.getPropertiesValue("encrypt_ftp_port"));
			 final String ftp_user = BaseToolsUtils.getPropertiesValue("encrypt_ftp_user");
			 final String ftp_pwd = BaseToolsUtils.getPropertiesValue("encrypt_ftp_pwd");
			 final String ftp_path = BaseToolsUtils.getPropertiesValue("encrypt_ftp_path");
	        ChannelSftp sftp = null;  
	        try {  
	            JSch jsch = new JSch();  
	            jsch.getSession(ftp_user, ftp_ip, ftp_port);  
	            Session sshSession = jsch.getSession(ftp_user, ftp_ip, ftp_port);  
	            System.out.println("Session created.");  
	            sshSession.setPassword(ftp_pwd);  
	            Properties sshConfig = new Properties();  
	            sshConfig.put("StrictHostKeyChecking", "no");  
	            sshSession.setConfig(sshConfig);  
	            sshSession.connect();  
	            System.out.println("Session connected.");  
	            System.out.println("Opening Channel.");  
	            Channel channel = sshSession.openChannel("sftp");  
	            channel.connect();  
	            sftp = (ChannelSftp) channel;  
	            System.out.println("Connected to " + ftp_ip + ".");  
	        } catch (Exception e) {  
	            e.printStackTrace() ;  
	        }  
	        return sftp;  
	    }  
}
